package com.ciwong.epaper.modules.reciteWords.ui;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.reciteWords.adapter.FragmentAdapter;
import com.ciwong.epaper.modules.reciteWords.ui.fragment.TabLearnedFragment;
import com.ciwong.epaper.modules.reciteWords.ui.fragment.TabUnlearnFragment;
import com.ciwong.epaper.widget.evaluate.WorkViewPager;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.mobilelib.ui.BaseActivity;

import java.util.ArrayList;

/**
 * 单词列表
 */
public class ReciteWordLisActivity extends BaseActivity {

    private String TAG = "WordLisActivity";
    private WorkViewPager mVp;
    private TabLayout mTabLayout;
    public TextView tvWordNum;
    private FragmentAdapter adapter;
    private static final int TAB_POSITION_0 = 0;
    private static final int TAB_POSITION_1 = 1;
    private TabLearnedFragment tabLearnedFragment;
    private TabUnlearnFragment tabUnlearnFragment;
    private String tittle="单词列表";
    private String strCount="单词数量：";

    @Override
    protected int setView() {
        return R.layout.activity_recite_word_list;
    }

    @Override
    protected void findViews() {
        //实例化
        mVp = (WorkViewPager) findViewById(R.id.vp_word_list);
        mTabLayout = (TabLayout) findViewById(R.id.tab_word_list);
        tvWordNum = (TextView) findViewById(R.id.tv_word_num);
    }

    public void init() {
        setTitleText(tittle);
        initViewPager();
        initEvent();
    }

    @Override
    protected void loadData() {}

    private void initViewPager() {
        //页面，数据源
        ArrayList<Fragment> list = new ArrayList<>();
        tabLearnedFragment = new TabLearnedFragment();
        tabUnlearnFragment = new TabUnlearnFragment();
        list.add(tabLearnedFragment);
        list.add(tabUnlearnFragment);
        //ViewPager的适配器
        adapter = new FragmentAdapter(getSupportFragmentManager(), this, list);
        mVp.setAdapter(adapter);
        mVp.setCanScroll(false);
        //绑定
        mTabLayout.setupWithViewPager(mVp);
        //处理indecator长度
        mTabLayout.post(new Runnable() {
            @Override
            public void run() {
                adapter.setIndicator(mTabLayout, 8, 8);
            }
        });
        //设置自定义视图
        for (int i = 0; i < mTabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = mTabLayout.getTabAt(i);
            tab.setCustomView(adapter.getTabView(i));
        }

    }

    public void initEvent() {
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tabSelected(tab);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void tabSelected(TabLayout.Tab tab) {
        int wordNum = 0;
        int selectedTabPosition = tab.getPosition();
        if (TAB_POSITION_0 == selectedTabPosition) {
            wordNum = tabLearnedFragment.getWordNum();
        } else if (TAB_POSITION_1 == selectedTabPosition) {
            tabUnlearnFragment.initData();
            wordNum = tabUnlearnFragment.getWordNum();
        }
        CWLog.w(TAG, "onTabSelected:" + tab.getPosition() + strCount+ wordNum);
        showWordCount(wordNum);
    }
    public void showWordCount(int count){
        CWLog.w(TAG,  strCount+ count);
        if (count <= 0) {
            tvWordNum.setVisibility(View.GONE);
        } else {
            tvWordNum.setVisibility(View.VISIBLE);
            tvWordNum.setText(strCount + count);
        }
    }
}
